package com.jamin.aabill.dao;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.jamin.aabill.data.UserInfo;
import com.jamin.aabill.db.BillDataBaseHelper;
import com.jamin.aabill.db.UserInfoTableMetaData;
import com.jamin.aabill.ui.AABillApplication;
import com.jamin.aabill.utils.Utils;

public class UserInfoDao {

	
	private static final String TAG = "UserInfoDao";
	BillDataBaseHelper mDbHelper = null;


	public UserInfoDao(){
		mDbHelper = AABillApplication.mDB;
	}


	/**
	 * 插入用户数据<br>
	 * @param userInfo
	 */
	public long insert(UserInfo userInfo){
		SQLiteDatabase db = mDbHelper.getWritableDatabase();
		long id = db.insert(UserInfoTableMetaData.TABLE_NAME, 
				null, getContentValues(userInfo));
		db.close();
		return id;
	}

	
	
	
	public void update(UserInfo userInfo){
		SQLiteDatabase db = mDbHelper.getWritableDatabase();
		db.update(UserInfoTableMetaData.TABLE_NAME, 
				 getContentValues(userInfo) ,"" , null );
		db.close();
	}
	
	/**
	 * 查询用户数据<br>
	 * @return
	 */
	public UserInfo getUserInfo(){
		UserInfo mUserInfo = null;
		SQLiteDatabase db = mDbHelper.getReadableDatabase();
		Cursor mCur = db.query(
				UserInfoTableMetaData.TABLE_NAME, 
				null, null, null, null, null, null);
		if(mCur != null ){
			if(mCur.getCount() != 0){
				mCur.moveToFirst();
				return new UserInfo(mCur);
			}
			mCur.close();
		}
		return mUserInfo;
	}


	/**
	 * 删除数据库<br>
	 */
	public void deleteUserInfo(){
		SQLiteDatabase db = mDbHelper.getWritableDatabase();
		db.delete(UserInfoTableMetaData.TABLE_NAME,null, null);
		db.close();
	}
	
	
	
	
	private ContentValues getContentValues(UserInfo userInfo){
		ContentValues values = new ContentValues();
		values.put(UserInfoTableMetaData._ID, userInfo.getID());
		values.put(UserInfoTableMetaData.NICK_NAME, userInfo.getName());
		values.put(UserInfoTableMetaData.EMAIL, userInfo.getEmail());
		values.put(UserInfoTableMetaData.MOBILE, userInfo.getPhoneNum());
		values.put(UserInfoTableMetaData.CREATE_AT, userInfo.getCreate_at());
		values.put(UserInfoTableMetaData.LAST_LOGIN_AT, userInfo.getLastLogin_at());
		return values;
	}


}
